package seqlist.link.leetcode;

/**
 * @author yuisama
 * @date 2023/03/19 11:18
 * 相交链表
 **/
public class Num160_IntersectionList {
    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        // 引入两个引用listA和listB，同时开始遍历
        // 若listA走到当前链表终点，倒过头再从链表B开始走 x + z + y = y + z + x
        ListNode l1 = headA,l2 = headB;
        while (l1 != l2) {
            l1 = l1 == null?headB : l1.next;
            l2 = l2 == null?headA : l2.next;
        }
        return l1;
    }
}